<?php

namespace Home\Model;

use Think\Model;

class SearchLogModel extends Model {

	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = '', $groupby = '') {
		$page = $page < 1 ? 1 : $page;
		$num = $num ? $num : C ( 'page_num' );

		// 读取缓存
		$cacheName = get_cache_name ( $_GET ['mobile'].'searchlog_' . $where . $page . $order . $num . $groupby );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}

		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->group ( $groupby )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] ['url'] = C ( 'site_url' ) . '/name/' . $v ['keyword'] . '/';
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['page'] = page_html ( $result ['total_num'], $page, $num );
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		if ($page > 1) {
			$result ['prev_page'] = $page - 1;
		} else {
			$result ['prev_page'] = '';
		}
		S ( $cacheName, $result, C ( 'cache_time_short' ) );
		return $result;
	}

	/**
	 * 添加
	 *
	 * @param unknown $data
	 * @return Ambigous <\Think\mixed, boolean, string, unknown>
	 */
	public function saveData($data) {
		$data ['createtime'] = date ( "Y-m-d H:i:s" );
		$data ['agent'] = $_SERVER ['HTTP_USER_AGENT'];
		$data ['from_url'] = $_SERVER ['HTTP_REFERER'];
		$data ['uid'] = get_cookie ( 'uid' );
		if (stripos ( $data ['agent'], 'spide' ) || !$data ['from_url']) {
			return false;
		}
		$result = $this->add ( $data );
		return $result;
	}
}
?>